var selectedDataRow = '', selectedRecordRow = '';
var isInit = true;
var totalData;
var vueObj = new Vue({
    el: "#vue",
    data: {
        orderInfo: searchInfo
    }
});

$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedDataRow = row;
            openQueryOrderPage();
        },
        'click .isvInfoView': function(e, value, row, index) {
            //编辑操作
            selectedDataRow = row;
            openCommonIsvPage(row.isvPid, 'org');
        },
    };
    laydate.render({
        elem: '#gmtCreateStr'
        ,type: 'datetime'
        ,range: true //或 range: '~' 来自定义分割字符
        // ,value: dateFtt("yyyy-MM-dd hh:mm:ss", preDate) + ' - ' + dateFtt("yyyy-MM-dd hh:mm:ss", curDate)
        //js赋值重置失效，改为就是怕、页面赋值
        ,isInitValue: true //是否允许填充初始值，默认为 true
    });
    initTextSearch("#isvName",
        basePath + 'bizContentOrder/getIsvData?t='+new Date().getTime()+'&isvName=',
        ['isvPid', 'isvName'],
        {'isvPid':'服务商PID', 'isvName':'服务商名称'},
        'isvPid',
        'isvName',
        true,
        selectIsvValue,
        noSelectIsvValue,
        false);
});

function selectIsvValue(data) {
    if (!data || !data.isvPid) {
        return;
    }
    $('#isvPid').val(data.isvPid);
}

function noSelectIsvValue() {
    $('#isvPid').val('');
}

function doSelectBiz() {
    var bizCode = $('#bizCode').val();
    if (!bizCode || bizCode == '') {
        $('#cBizCode').val('');
        var str = '<option value="">--请选择--</option>';
        $('#cBizCode').html(str);
        doRefresh();
    } else {
        var postData = {
            bizParentCode : bizCode,
        };
        $.ajax({
            type: 'post',
            url: basePath + 'bizContentOrder/selectBiz?t=' + new Date().getTime(),
            dataType: 'json',
            data: postData,
            success : function (data) {
                if(data && data.result){
                    setBizSelect(data.content);
                }else if(data&&!data.result){
                    toastr.error(data.message);
                }else{
                    toastr.error('获取服务项信息失败');
                }
            },
            error: function (request) {
                delayToastr(common_system_error);
            }
        });
    }
}

function setBizSelect(bizList) {
    $('#cBizCode').val('');
    var htmlStr = '<option value="">--请选择--</option>';
    if (bizList) {
        for (var i=0; i<bizList.length; i++) {
            htmlStr += '<option value="'+bizList[i].code+'">'+bizList[i].name+'</option>';
        }
    }
    $('#cBizCode').html(htmlStr);
    doRefresh();
}

function resetCbizCode(bizCode) {
    var postData = {
        bizParentCode : bizCode,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'bizContentOrder/selectBiz?t=' + new Date().getTime(),
        dataType: 'json',
        data: postData,
        success : function (data) {
            if(data && data.result){
                // setBizSelect(data.content);
                var bizList = data.content;
                var htmlStr = '<option value="">--请选择--</option>';
                if (bizList) {
                    for (var i=0; i<bizList.length; i++) {
                        htmlStr += '<option value="'+bizList[i].code+'">'+bizList[i].name+'</option>';
                    }
                }
                $('#cBizCode').html(htmlStr);
                $("#cBizCode").val('course');
            }else if(data&&!data.result){
                toastr.error(data.message);
            }else{
                toastr.error('获取服务项信息失败');
            }
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

function doRefresh() {
    selectedDataRow = '';
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'bizContentOrder/pageFindOrderData?t=' + new Date().getTime()
    });
}

function getParams(params) {
    var postData = {};
    $("#searchForm").serializeArray().map(function (value,index,arr) {
        postData[value.name] = value.value;
    });
    var timeInterval = $("#gmtCreateStr").val();
    var gmtCreateStartStr = '';
    var gmtCreateEndStr = '';
    if(timeInterval){
        var dates = timeInterval.split(' - ');
        if(dates && dates.length >1){
            gmtCreateStartStr = dates[0];
            gmtCreateEndStr = dates[1];
        }
    }
    postData.gmtCreateStartStr = gmtCreateStartStr;
    postData.gmtCreateEndStr = gmtCreateEndStr;

    postData.pageSize = params.pageSize;
    postData.pageNo = params.pageNumber;
    if (isInit) {
        isInit = false;
        postData.pageSize = searchInfo.pageSize ? searchInfo.pageSize : params.pageSize;
        postData.pageNo = searchInfo.pageNo ? searchInfo.pageNo : params.pageNumber;
    }
    return postData;
}


function handleServerResult(res) {
    totalData = res.totalData;
    return {
        "total": res.pageInfo.total,//总页数
        "rows": res.pageInfo.list   //数据
    };
}

function openQueryOrderPage() {
    if (!selectedDataRow || selectedDataRow == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    if(selectedDataRow.id == '' || selectedDataRow.id == undefined){
        toastr.error('该订单无详细信息，请重新选择');
        return;
    }
    openEditPage(selectedDataRow.id);
}

function openEditPage(id) {
    //   $('#searchForm').serialize()  错误发生在不是String类型的空数据 除去from中的空数据
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    var cBizName = selectedDataRow.cBizName;
    window.location.href = basePath + 'bizContentOrder/queryOrderPage?id=' + id + '&cBizName='+ cBizName + '&t=' + new Date().getTime() +
        '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function doExport() {
    $('#searchForm').attr("action",basePath+"bizContentOrder/exportOrderDataExcel?t=" + new Date().getTime());
    $('#searchForm').submit();
}

//------------------------------------------------------------------------列表数据初始化
function getNameFromCode(map, code) {
    if (map) {
        for (var i in map) {
            if (map[i].code == code) {
                return map[i].name;
            }
        }
    }
    return code;
}

function formatStatus(value, row, index) {
    if($("#cBizCode").val() == 'inquiry'){
        if (inquiryStatusMap) {
            return inquiryStatusMap[value];
        }
    }else{
       if (statusMap) {
            return statusMap[value];
       }
    }
    return value;
}

function formatChannel(value, row, index){
    return getNameFromCode(channelMap, value);
}

function clearForm(formId){
    var formObj = $("#" + formId);
    if(formId && formObj){
        formObj.clearForm(true);
    }
    $("#bizCode").val('JKNR');
    resetCbizCode('JKNR');
}

function formatPayStatus(value, row, index) {
    if (payStatusMap) {
        return payStatusMap[value];
    }
    return value;
}

function formatSource(value, row, index) {
    return getNameFromCode(sourceMap, value);
}

//------------------------------------------------------------------------列表选中事件
//左边选中事件
$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedDataRow = row;
});

$("#dataTable").on("load-success.bs.table",function(data){
    $('#dataTable').bootstrapTable('insertRow', {
        index: 0,
        row: totalData
    });
});